package com.ly.base;



import com.google.common.collect.Sets;
import org.springframework.data.domain.Sort;

import java.util.Set;

/**
 * 排序生成器
 * @author ：LY
 * @date ：Created in 2020/11/16 20:30
 * @modified By：
 */
public class HouseSort {
    public static  final String DEFAULT_SORT_KEY = "lastUpdateTime";

    public static  final String DISTANCE_TO_SUBWAY_KEY = "distanceToSubway";

    public static  final Set<String> SORT_KEYS = Sets.newHashSet(
            DEFAULT_SORT_KEY,
            "creatTime",
            "price",
            "area",
            DISTANCE_TO_SUBWAY_KEY
    );
    public static Sort generateSort(String key,String directionKey){
        key = getSortKey(key);


        Sort.Direction direction = Sort.Direction.fromStringOrNull(directionKey);
        if (direction == null){
            direction = Sort.Direction.DESC;
        }
        return new Sort(direction,key);
    }
    public static String getSortKey(String key){
        if (!SORT_KEYS.contains(key)){
            key = DEFAULT_SORT_KEY;
        }
        return key;
    }
}
